package com.ruoyi.web.controller.system;

import cn.hutool.core.date.DateUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.*;
import com.ruoyi.common.pojo.*;
import com.ruoyi.system.service.SysBoatEventlogService;
import com.ruoyi.system.service.SysTaskService;
import com.ruoyi.web.service.DeviceService;
import com.ruoyi.web.service.IWisdomCruiseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

@Api(tags = "2.4河长巡河")
@RestController
@RequestMapping("/system/task")
public class SysTaskController {

    @Resource
    SysTaskService sysTaskService;

    @Resource
    IWisdomCruiseService wisdomCruiseService;

    @Resource
    private DeviceService deviceService;

    @Resource
    SysBoatEventlogService sysBoatEventlogService;

    /*@ApiOperation("开始无人船智慧巡河任务")
    @PostMapping("/startTask")

    public AjaxResult startTask(@RequestBody SysCruiseParam param) {
        return wisdomCruiseService.startTask(param);
    }*/

    /*@ApiOperation("结束无人船智慧巡河任务")
    @PostMapping("/stopTask")

    public AjaxResult stopTask(@RequestBody SysCruiseParam param) {
        return wisdomCruiseService.stopTask(param);
    }*/

    @ApiOperation("请求控制")
    @PostMapping("/requestControl")
    public AjaxResult requestControl(@RequestBody RequestControlParam param) {
        AjaxResult ajaxResult = wisdomCruiseService.requestControl(param);
        return ajaxResult;
    }

    @ApiOperation("急停巡河任务")
    @PostMapping("/stopTask")
    public AjaxResult stopTask(@RequestBody SysTask param) {
        AjaxResult ajaxResult = wisdomCruiseService.emergencyStopTask(param);
        return ajaxResult;
    }

    @ApiOperation("改变船方向")
    @PostMapping("/changeBoatDirection")
    public AjaxResult changeBoatDirection(@RequestBody BoatDirectionParam param) {
        AjaxResult ajaxResult = sysTaskService.changeBoatDirection(param);
        //模拟程序需要后调用
        wisdomCruiseService.changeBoatDirection(param);
        return ajaxResult;
    }

    @ApiOperation("切换智慧巡河任务模式")
    @PostMapping("/switchTask")
    public AjaxResult switchTask(@RequestBody SysTask param) {
        AjaxResult ajaxResult = sysTaskService.switchTaskModel(param);
        //模拟程序需要后调用
        wisdomCruiseService.switchTaskModel(param);

        SysTask sysTask = sysTaskService.getById(param.getId());
        SysBoatEventlog sysBoatEventlog = new SysBoatEventlog();
        sysBoatEventlog.setTaskId(sysTask.getId());
        sysBoatEventlog.setCommandText("寻迹");
        sysBoatEventlog.setCruiseId(sysTask.getObjectId());
        sysBoatEventlog.setCreateTime(DateUtil.date());
        sysBoatEventlogService.save(sysBoatEventlog);

        ModelSwitchPush modelSwitchPush = new ModelSwitchPush();
        modelSwitchPush.setTaskId(param.getId());
        modelSwitchPush.setModelType(23);
        deviceService.sendMessage("modelSwitch", modelSwitchPush);
        return ajaxResult;
    }

    @ApiOperation("切换污染源模式")
    @PostMapping("/switchToPollute")
    public AjaxResult switchToPollute(@RequestBody SysTaskPollute param) {
        AjaxResult ajaxResult = wisdomCruiseService.switchToPollute(param);
        return ajaxResult;
    }

    @ApiOperation("2.4.1启动河长巡河")
    @ApiOperationSupport(order = 1)
    @PostMapping("/startUserPatrolRiver")
    public AjaxResult startUserPatrolRiver(@RequestBody StartUserPatrolRiverReq param) {
        return sysTaskService.startUserPatrolRiver(param);
    }

    @ApiOperation("2.4.2暂停河长巡河")
    @ApiOperationSupport(order = 2)
    @PostMapping("/pauseUserPatrolRiver")
    public AjaxResult pauseUserPatrolRiver(@RequestBody PauseUserPatrolRiverReq param) {
        return sysTaskService.pauseUserPatrolRiver(param);
    }

    @ApiOperation("2.4.3恢复河长巡河")
    @ApiOperationSupport(order = 3)
    @PostMapping("/restoreUserPatrolRiver")
    public AjaxResult restoreUserPatrolRiver(@RequestBody PauseUserPatrolRiverReq param) {
        return sysTaskService.restoreUserPatrolRiver(param);
    }

    @ApiOperation("2.4.4停止河长巡河")
    @ApiOperationSupport(order = 4)
    @PostMapping("/stopUserPatrolRiver")
    public AjaxResult stopUserPatrolRiver(@RequestBody PauseUserPatrolRiverReq param) {
        return sysTaskService.stopUserPatrolRiver(param);
    }

    @ApiOperation("2.4.5巡河内容-添加巡河记录")
    @ApiOperationSupport(order = 5)
    @PostMapping("/addUserPatrolRiverRecord")
    public AjaxResult addUserPatrolRiverRecord(@RequestBody AddUserPatrolRiverRecordReq param) {
        return sysTaskService.addUserPatrolRiverRecord(param);
    }

    @ApiOperation("2.4.6巡河内容-查询巡河记录详情")
    @ApiOperationSupport(order = 6)
    @GetMapping("/getUserPatrolRiverRecordDetail")
    public AjaxResult getUserPatrolRiverRecordDetail(Long id) {
        return sysTaskService.getUserPatrolRiverRecordDetail(id);
    }

    @ApiOperation("2.4.7巡河内容-查询我的巡河记录")
    @ApiOperationSupport(order = 7)
    @GetMapping("/getMyUserPatrolRiverRecord")
    public AjaxResult getMyUserPatrolRiverRecord(@ApiParam(value = "0：待处理；1：已处理，不传查全部") @RequestParam(value = "issuestate", required = false) Integer issuestate,
                                                 @ApiParam(value = "当前记录起始索引", required = true) @RequestParam(value = "pageNum") Integer pageNum,
                                                 @ApiParam(value = "每页显示记录数", required = true) @RequestParam(value = "pageSize") Integer pageSize) {
        return sysTaskService.getMyUserPatrolRiverRecord(issuestate, pageNum, pageSize);
    }

    @ApiOperation("2.4.8巡河内容-综合巡河记录")
    @ApiOperationSupport(order = 8)
    @GetMapping("/searchUserPatrolRiverRecord")
    public AjaxResult searchUserPatrolRiverRecord(@ApiParam(value = "0：待处理；1：已处理，不传查全部") @RequestParam(value = "issuestate", required = false) Integer issuestate,
                                                  @ApiParam(value = "开始时间") @RequestParam(value = "startTime", required = false) Date startTime,
                                                  @ApiParam(value = "结束时间") @RequestParam(value = "endTime", required = false) Date endTime,
                                                  @ApiParam(value = "内容") @RequestParam(value = "keywords", required = false) String keywords,
                                                  @ApiParam(value = "当前记录起始索引", required = true) @RequestParam(value = "pageNum") Integer pageNum,
                                                  @ApiParam(value = "每页显示记录数", required = true) @RequestParam(value = "pageSize") Integer pageSize) {
        return sysTaskService.searchUserPatrolRiverRecord(issuestate, startTime, endTime, keywords, pageNum, pageSize);
    }

    @ApiOperation("2.4.9巡河内容-编辑巡河记录")
    @ApiOperationSupport(order = 9)
    @PostMapping("/editUserPatrolRiverRecord")
    public AjaxResult editUserPatrolRiverRecord(@RequestBody EditUserPatrolRiverRecordReq param) {
        return sysTaskService.editUserPatrolRiverRecord(param);
    }

    @ApiOperation("2.4.10巡河内容-删除巡河记录")
    @ApiOperationSupport(order = 10)
    @PostMapping("/deleteUserPatrolRiverRecord")
    public AjaxResult deleteUserPatrolRiverRecord(@RequestBody DeleteUserPatrolRiverRecordReq param) {
        return sysTaskService.deleteUserPatrolRiverRecord(param);
    }

    @ApiOperation("2.4.12查询巡河轨迹")
    @ApiOperationSupport(order = 12)
    @GetMapping("/查询巡河轨迹")
    public AjaxResult getTaskPatrolRiverRoute(GetTaskPatrolRiverRouteReq param) {
        return sysTaskService.getTaskPatrolRiverRoute(param);
    }

    @ApiOperation("2.4.13巡河内容-查询我的巡河记录")
    @ApiOperationSupport(order = 13)
    @GetMapping("/getMyUserPatrolRiver")
    public AjaxResult getMyUserPatrolRiver(GetMyUserPatrolRiverReq param) {
        return sysTaskService.getMyUserPatrolRiver(param);
    }

    @ApiOperation("2.4.14巡河任务-查询巡河任务详情")
    @ApiOperationSupport(order = 14)
    @GetMapping("/getPatrolRiverTaskDetail")
    public AjaxResult getPatrolRiverTaskDetail(GetPatrolRiverTaskDetailReq param) {
        return sysTaskService.getPatrolRiverTaskDetail(param);
    }

    @ApiOperation("创建巡河任务")
    @PostMapping("/createTask")
    public AjaxResult createTask(@RequestBody SysTask param) {
        AjaxResult ajaxResult = sysTaskService.createTask(param);
        //模拟程序需要后调用
        wisdomCruiseService.applyControl(1, 0);
        wisdomCruiseService.startTask(param);
        //推送任务开始
        CreateTaskPush taskPush = new CreateTaskPush();
        taskPush.setTaskId(param.getId());
        deviceService.sendMessage("newTask", taskPush);
        return ajaxResult;
    }

    @ApiOperation("结束巡河任务")
    @PostMapping("/closeTask")
    public AjaxResult closeTask(@RequestBody SysTask param) {
        AjaxResult ajaxResult = sysTaskService.closeTask(param);
        //模拟程序需要后调用
        wisdomCruiseService.applyControl(0, 0);
        wisdomCruiseService.stopTask(param);
        return ajaxResult;
    }

    @ApiOperation("切换巡河规划路径")
    @PostMapping("/switchPlanRoute")
    public AjaxResult switchPlanRoute(@RequestBody SwitchPlanRouteReq param) {
        return sysTaskService.switchPlanRoute(param);
    }

    @ApiOperation("上传巡河任务轨迹")
    @PostMapping("/uploadTrack")
    public AjaxResult uploadTrack(@RequestBody SysTaskRoute param) {
        return sysTaskService.uploadTrack(param);
    }

    @ApiOperation("上传巡河发现的问题")
    @PostMapping("/addIssue")
    public AjaxResult addIssue(@RequestBody SysTaskIssue param) {
        return sysTaskService.addIssue(param);
    }

    @ApiOperation("获取巡河发现的问题列表")
    @GetMapping("/getIssue")
    public AjaxResult getIssue(GetTaskIssueReq param) {
        return sysTaskService.getIssue(param);
    }

    @ApiOperation("删除巡河发现问题")
    @GetMapping("/deleteIssue")
    public AjaxResult deleteIssue(Long issueId) {
        return sysTaskService.deleteIssue(issueId);
    }

    @ApiOperation("更新巡河发现问题")
    @PostMapping("/updateIssue")
    public AjaxResult updateIssue(@RequestBody SysTaskIssue param) {
        return sysTaskService.updateIssue(param);
    }

    @ApiOperation("获取问题类型列表")
    @GetMapping("/getIssueTypeList")
    public AjaxResult getIssueTypeList() {
        return sysTaskService.getIssueTypeList();
    }

    @ApiOperation("删除巡河任务")
    @PostMapping("/deleteTrack")
    public AjaxResult deleteTrack(@RequestBody SysTask param) {
        return sysTaskService.deleteTrack(param);
    }

    @ApiOperation("删除巡河任务附件")
    @PostMapping("/deleteFile")
    public AjaxResult deleteFile(@RequestBody SysTaskFile param) {
        return sysTaskService.deleteFile(param);
    }

    @ApiOperation("获取巡河任务列表")
    @PostMapping("/queryTaskList")
    public AjaxResult queryTaskList(@RequestBody QueryTaskListReq param) {
        return sysTaskService.queryTaskList(param);
    }

    @ApiOperation("获取巡河实际轨迹")
    @PostMapping("/getTaskRoute")
    public AjaxResult getTaskRoute(@RequestBody SysTask param) {
        return sysTaskService.getTaskRoute(param);
    }


}
